package com.infore.siteManagement.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.infore.siteManagement.entity.Equipment;

import feign.Param;

import java.lang.String;
/**
 * 设备仓库
 * @author 刘乐法
 *
 *
 */
@Repository
public interface IEquipmentRepository extends JpaRepository<Equipment, String>,JpaSpecificationExecutor<Equipment>{

	@Modifying
	@Transactional
	int deleteByEquipmentIdIn(List<String> ids);

	@EntityGraph("equipment_view")
	Equipment findOneByEquipmentId(String equipmentid);
	
	@EntityGraph("equipment_view")
	List<Equipment> findAllByEquipmentIdIn(List<String> ids);
	
	@EntityGraph("equipment_view")
	List<Equipment> findAll();
	
	@EntityGraph("equipment_view")
	List<Equipment> findAll(Specification<Equipment> spec);
	
	@EntityGraph("equipment_view")
	Page<Equipment> findAll(Pageable pageable);
	
	@EntityGraph("equipment_view")
	Page<Equipment> findAll(Specification<Equipment> spec,Pageable pageable);

	@Query(value = "select t.equipment_id from EQUIPMENT t where t.station_mn in(:mn) ", nativeQuery = true)
	List<String> findIdsByStationMn(@Param(value = "mn")List<String> mn);

	/** 插入仪器部分数据 **/
	@Modifying
	@Transactional
	@Query(value = "INSERT INTO EQUIPMENT (EQUIPMENT_ID,EQUIPMENT_NAME,MANUFACTURE_CODE,MANUFACTOR,ISENABLE,"
			+ "STATION_MN,STATION_NAME,CREATE_TIME,EQUIPMENT_MODEL,EQUIPMENT_ORANGE) VALUES(:equipmentId,:equipmentName,:manufactureCode,:manufactor,:isenable,:stationMn,:stationName,:createTime,:equipmentModel,:equipmentOrange)",nativeQuery = true)
	int insertEquipment(@Param(value = "equipmentId")String equipmentId,
		@Param(value = "equipmentName")String equipmentName,
		@Param(value = "manufactureCode")String manufactureCode,
		@Param(value = "manufactor")String manufactor,
		@Param(value = "isenable")String isenable,
		@Param(value = "stationMn")String stationMn,
		@Param(value = "stationName")String stationName,
		@Param(value = "createTime")Date createTime,
		@Param(value = "equipmentModel")String equipmentModel,
		@Param(value = "equipmentOrange")String equipmentOrange);
	
	/** 更新仪器部分数据 **/
	@Modifying
	@Transactional
	@Query(value = "UPDATE EQUIPMENT SET EQUIPMENT_NAME = :equipmentName,MANUFACTURE_CODE = :manufactureCode,"
			+ "MANUFACTOR = :manufactor,ISENABLE = :isenable,STATION_MN = :stationMn,STATION_NAME = :stationName,CREATE_TIME = :createTime,EQUIPMENT_MODEL=:equipmentModel,EQUIPMENT_ORANGE=:equipmentOrange WHERE EQUIPMENT_ID = :equipmentId",nativeQuery = true)
	int updateEquipment(@Param(value = "equipmentId")String equipmentId,
		@Param(value = "equipmentName")String equipmentName,
		@Param(value = "manufactureCode")String manufactureCode,
		@Param(value = "manufactor")String manufactor,
		@Param(value = "isenable")String isenable,
		@Param(value = "stationMn")String stationMn,
		@Param(value = "stationName")String stationName,
		@Param(value = "createTime")Date createTime,
		@Param(value = "equipmentModel")String equipmentModel,
		@Param(value = "equipmentOrange")String equipmentOrange);
}







